import request from "@/utils/request";
import {fetchEventSource} from "@microsoft/fetch-event-source";

export default {
  //获取应用参数
  parameters(appUuid){
    return request.get('api/ai/chat/parameters',{params:{appUuid}})
  },
  //获取应用Meta信息
  meta(appUuid){
    return request.get('api/ai/chat/meta',{params:{appUuid}})
  },
  //获取应用基本信息
  info(appUuid){
    return request.get('api/ai/chat/info',{params:{appUuid}})
  },
  // 获取对话列表
  conversations(pra) {
    return request.get('api/ai/chat/conversations', {params:pra})
  },
  // 获取历史消息
  messages(pra) {
    return request.get('api/ai/chat/messages', {params:pra})
  },
  // 重命名
  rename(pra){
    return request.post('api/ai/chat/conversations/name',pra);
  },
  // 停止相应
  stop(pra){
    return request.post('api/ai/chat/chat-messages/stop',pra);
  },
  // 删除对话
  del(pra){
    return request.delete('api/ai/chat/conversations/delete',{params:pra});
  },
  send(pra, callback) {
    if(callback == null) callback ={};
    var url = 'http://192.168.100.108:1900/api/ai/chat/chat-messages';
    var token = "";
    fetchEventSource(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        "token": token
      },
      body: JSON.stringify(pra),
      openWhenHidden:true,
      onopen(response) {
        if (callback.open != null) {
          callback.open(response);
        }
      },
      onmessage(msg) {
        if (callback.message != null) {
          callback.message(msg);
        }
      },
      onclose() {
        if (callback.close != null) {
          callback.close();
        }
      },
      onerror(err) {
        if (callback.error != null) {
          callback.error(err);
        }
      },
    })
  },
}
